package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Hotel;
import cn.wolfcode.trip.base.query.HotelQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IHotelService;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/hotel")
public class HotelController {
    @Autowired
    private IHotelService hotelService;
    @Autowired
    private IRegionService regionService;


    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") HotelQueryObject qo, Model model) {
        model.addAttribute("pageInfo",hotelService.query(qo));
        model.addAttribute("regions",regionService.list(null));
        return "hotel/list";
    }

    /*
    admin端新增和更新酒店
     */
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Hotel entity, MultipartFile img) {
        //先处理上传图片的问题
        if (img != null && img.getContentType().startsWith("image") && img.getSize() > 0) {
            //把图片上传到本地图库
            String url = UploadUtil.upload(img);
            //替换旧的图片地址
            entity.setCoverUrl(url);
        }
        hotelService.saveOrUpdate(entity);
        return "redirect:/hotel/list.do";
    }

}
